package com.shujia.flink.sink;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.connector.file.sink.FileSink;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Demo02FileSink {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setRuntimeMode(RuntimeExecutionMode.BATCH);

        // 读取students文件 统计班级人数 将结果保存到文件中
        DataStream<String> studentDS = env.readTextFile("flink/data/students.txt");

        DataStream<Tuple2<String, Integer>> clazzCnt = studentDS
                .map(line -> Tuple2.of(line.split(",")[4], 1), Types.TUPLE(Types.STRING, Types.INT))
                .keyBy(kv -> kv.f0)
                .sum(1);

//        clazzCnt.print(); // 本质上也是一种sink，sink到console

        // 整理结果数据，将元组转换成String字符串以便于保存到文件当中
        DataStream<String> resultDS = clazzCnt.map(kv -> kv.f0 + "\t" + kv.f1);

        FileSink<String> fileSink = FileSink
                .forRowFormat(
                        new Path("flink/data/clazz_cnt")
                        , new SimpleStringEncoder<String>("utf-8")
                ).build();

        // 使用sink
        resultDS.sinkTo(fileSink);

        env.execute();

    }
}
